#include "Frustum.h"

const size_t NUM_OF_PLANES = 6;

/*-----------------------------------------------------------------------------------------------*/
Frustum::Frustum()
{
   m_pPlanes = new D3DXPLANE[NUM_OF_PLANES];
}
/*-----------------------------------------------------------------------------------------------*/
bool Frustum::calculatePlanes(const D3DXMATRIX* pViewMat, const D3DXMATRIX* pProjMat)
{
   if (!pProjMat || !pProjMat )
   {
      std::cout << "Frustum::calculatePlanes" << std::endl; 
      return false;
   }

   D3DXMATRIX mat;

   D3DXMatrixMultiply(&mat, pViewMat, pProjMat);

   //Left Plane
   m_pPlanes[0].a = mat._14 + mat._11;
   m_pPlanes[0].b = mat._24 + mat._21;
   m_pPlanes[0].c = mat._34 + mat._31;
   m_pPlanes[0].d = mat._44 + mat._41;

   //Right Plane
   m_pPlanes[1].a = mat._14 - mat._11;
   m_pPlanes[1].b = mat._24 - mat._21;
   m_pPlanes[1].c = mat._34 - mat._31;
   m_pPlanes[1].d = mat._44 - mat._41;

   //Bottom Plane
   m_pPlanes[2].a = mat._14 + mat._12;
   m_pPlanes[2].b = mat._24 + mat._22;
   m_pPlanes[2].c = mat._34 + mat._32;
   m_pPlanes[2].d = mat._44 + mat._42;

   //Top Plane
   m_pPlanes[3].a = mat._14 - mat._12;
   m_pPlanes[3].b = mat._24 - mat._22;
   m_pPlanes[3].c = mat._34 - mat._32;
   m_pPlanes[3].d = mat._44 - mat._42;

   //Near plane
   m_pPlanes[4].a = mat._13;
   m_pPlanes[4].b = mat._23;
   m_pPlanes[4].c = mat._33;
   m_pPlanes[4].d = mat._43;

   //Far Plane
   m_pPlanes[5].a = mat._14 - mat._13;
   m_pPlanes[5].b = mat._24 - mat._23;
   m_pPlanes[5].c = mat._34 - mat._33;
   m_pPlanes[5].d = mat._44 - mat._43;

   for(size_t i = 0; i < NUM_OF_PLANES; ++i)
   {
      D3DXPlaneNormalize(&m_pPlanes[i], &m_pPlanes[i]);
   }

   return true;
}
/*-----------------------------------------------------------------------------------------------*/
bool Frustum::isBBoxColliding(const Vector3* pBoxVertices)
{
   //check every plane with every vertex 
   for (size_t i = 0; i < NUM_OF_PLANES; ++i)
   {
      //if any point is on any plane we show the node
      if(D3DXPlaneDotCoord(&m_pPlanes[i], &pBoxVertices[0]) >= 0.0f)
      {
         continue;
      }
      if(D3DXPlaneDotCoord(&m_pPlanes[i],&pBoxVertices[1]) >= 0.0f)
      {
         continue;
      }
      if(D3DXPlaneDotCoord(&m_pPlanes[i],&pBoxVertices[2]) >= 0.0f)
      {
         continue;
      }
      if(D3DXPlaneDotCoord(&m_pPlanes[i],&pBoxVertices[3]) >= 0.0f)
      {
         continue;
      }
      if(D3DXPlaneDotCoord(&m_pPlanes[i],&pBoxVertices[4]) >= 0.0f)
      {
         continue;
      }
      if(D3DXPlaneDotCoord(&m_pPlanes[i],&pBoxVertices[5]) >= 0.0f)
      {
         continue;
      }
      if(D3DXPlaneDotCoord(&m_pPlanes[i],&pBoxVertices[6]) >= 0.0f)
      {
         continue;
      }
      if(D3DXPlaneDotCoord(&m_pPlanes[i],&pBoxVertices[7]) >= 0.0f)
      {
         continue;
      }
      return false;
   }

   return true;
}
